<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * User Application: Permissions Management Class
 * Started: September 23, 2009 06:57 PM
 * Rewritten: Wed Oct 28, 2009 06:41 PM - Almost same time :o
 * Script version: 1.3
 +------------------------------------
 */
class perm{
	public function getValue($permission_required){
		global $view, $db, $lang, $_CF;
		if(!isset($this->keys, $this->values)){
			$q = $_CF->models->permissionkeys->select_all();
			while($a = $_CF->db->fetch_array($q)){
				$this->keys[$a['key']] = $a;
			}
			$q2 = $_CF->models->permissionvalues->select_all();
			while($a = $_CF->db->fetch_array($q2)){
				$this->values[$a['mask']][$a['key']] = $a;
			}
		}
		if(!isset($this->keys[$permission_required])){
			trigger_error("Permission Key {$permission_required} does not exist!");
		}
		if(isset($_SESSION["cf_username"], $_SESSION["cf_controller"])){
			$userController = unserialize($_SESSION["cf_controller"]);
			$group = $userController->group;
			if($group->getStatus() === false){
				trigger_error("Group assigned to user logged in does not exist!");
			}
			if(!isset($this->values[$group->getPermMaskID()][$permission_required])){
				$permission_value = $this->keys[$permission_required]["default_value"];
			}
			else{
				$permission_value = $this->values[$group->getPermMaskID()][$permission_required]["value"];
			}
		}
		else{
			$permission_value = $this->keys[$permission_required]["guest_value"];
		}
		return $permission_value;
	}
	
	public function noPermissionError(){
		global $view, $lang;
		$view->writeIn($view->inlineError($lang->getByKey("core.permdenied")));
	}
}
